<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="keyword"  content="Tooi">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        Dubbo + Zookeeper 实现微服务架构 - Tooi的博客 | Tooi-Blog
        
    </title>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/aircloud.css">
    <link rel="stylesheet" href="/css/gitment.css">
    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
</head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> 瞄准月亮。 </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>Tooi6</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archives">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#概述"><span class="toc-text">概述</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#微服务需要解决的问题"><span class="toc-text">微服务需要解决的问题</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Zookeeper"><span class="toc-text">Zookeeper</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#官网"><span class="toc-text">官网</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#什么是Zookeeper？"><span class="toc-text">什么是Zookeeper？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#部署Zookeeper"><span class="toc-text">部署Zookeeper</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#配置文件"><span class="toc-text">配置文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#zkClient"><span class="toc-text">zkClient</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Dubbo"><span class="toc-text">Dubbo</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#官网-1"><span class="toc-text">官网</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#什么是Dubbo？"><span class="toc-text">什么是Dubbo？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Spring-Boot-整合-Dubbo"><span class="toc-text">Spring Boot 整合 Dubbo</span></a></li></ol></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> 瞄准月亮。 </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        Dubbo + Zookeeper 实现微服务架构
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2020-01-03 22:36:02</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#Dubbo" title="Dubbo">Dubbo</a>
        <span>/</span>
        
        <a class="tag" href="/tags/#Zookeeper" title="Zookeeper">Zookeeper</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content ">
        <h3 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h3><h4 id="微服务需要解决的问题"><a href="#微服务需要解决的问题" class="headerlink" title="微服务需要解决的问题"></a>微服务需要解决的问题</h4><ul>
<li><strong>客户端如何访问这么多服务？</strong><blockquote>
<p>API 网关</p>
</blockquote>
</li>
<li><strong>服务与服务之间如何通信？</strong><blockquote>
<p>同步通信：HTTP/RPC<br>异步通信：（消息队列）kafka、RabbitMQ、RocketMQ  </p>
</blockquote>
</li>
<li><strong>这么多服务如何管理？</strong>  <blockquote>
<p>服务注册与发现：Zookeeper、eureka</p>
</blockquote>
</li>
<li><strong>服务挂了怎么办？</strong>  <blockquote>
<p>重试机制、服务熔断、服务降级、服务限流</p>
</blockquote>
</li>
</ul>
<h3 id="Zookeeper"><a href="#Zookeeper" class="headerlink" title="Zookeeper"></a>Zookeeper</h3><h4 id="官网"><a href="#官网" class="headerlink" title="官网"></a>官网</h4><blockquote>
<p><a href="https://zookeeper.apache.org/" target="_blank" rel="noopener">https://zookeeper.apache.org/</a></p>
</blockquote>
<h4 id="什么是Zookeeper？"><a href="#什么是Zookeeper？" class="headerlink" title="什么是Zookeeper？"></a>什么是Zookeeper？</h4><blockquote>
<p>Zookeeper 是 Apacahe Hadoop 的子项目，是一个树型的目录服务，支持变更推送，适合作为 Dubbo 服务的注册中心，工业强度较高，可用于生产环境，并推荐使用 </p>
</blockquote>
<h4 id="部署Zookeeper"><a href="#部署Zookeeper" class="headerlink" title="部署Zookeeper"></a>部署Zookeeper</h4><blockquote>
<p>下面使用 Docker 部署 Zookeeper 伪集群模式   </p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">version: &apos;3.1&apos;</span><br><span class="line"></span><br><span class="line">services:</span><br><span class="line">  zoo1:</span><br><span class="line">    image: zookeeper</span><br><span class="line">    restart: always</span><br><span class="line">    hostname: zoo1</span><br><span class="line">    ports:</span><br><span class="line">      - 2181:2181</span><br><span class="line">    environment:</span><br><span class="line">      ZOO_MY_ID: 1</span><br><span class="line">      ZOO_SERVERS: server.1=0.0.0.0:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181</span><br><span class="line"></span><br><span class="line">  zoo2:</span><br><span class="line">    image: zookeeper</span><br><span class="line">    restart: always</span><br><span class="line">    hostname: zoo2</span><br><span class="line">    ports:</span><br><span class="line">      - 2182:2181</span><br><span class="line">    environment:</span><br><span class="line">      ZOO_MY_ID: 2</span><br><span class="line">      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zoo3:2888:3888;2181</span><br><span class="line"></span><br><span class="line">  zoo3:</span><br><span class="line">    image: zookeeper</span><br><span class="line">    restart: always</span><br><span class="line">    hostname: zoo3</span><br><span class="line">    ports:</span><br><span class="line">      - 2183:2181</span><br><span class="line">    environment:</span><br><span class="line">      ZOO_MY_ID: 3</span><br><span class="line">      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181</span><br></pre></td></tr></table></figure>

<h4 id="配置文件"><a href="#配置文件" class="headerlink" title="配置文件"></a>配置文件</h4><blockquote>
<p>配置文件在 /conf/zoo.cfg </p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># 查看配置文件</span><br><span class="line">cat /conf/zoo.cfg</span><br><span class="line"></span><br><span class="line">dataDir=/data </span><br><span class="line">dataLogDir=/datalog</span><br><span class="line">tickTime=2000</span><br><span class="line">initLimit=5    # Zookeeper 接受客户端初始化连接时最长能忍受的心跳时间间隔数（当超过后表示连接失败） 5*2000 = 10s</span><br><span class="line">syncLimit=2    # Leader 与 Follower 之间发送消息，请求和响应时间长度 2*2000 = 4s</span><br><span class="line">autopurge.snapRetainCount=3    #指定了需要保留的文件数目。默认是保留 3 个。 </span><br><span class="line">autopurge.purgeInterval=0    #指定了清理频率，单位是小时，需要填写一个 1 或更大的整数，默认是 0，表示不开启自己清理功能。</span><br><span class="line">maxClientCnxns=60   # 限制连接到 Zookeeper 的客户端的数量，限制并发连接的数量，它通过 IP 来区分不同的客户端。（阻止Dos攻击）</span><br><span class="line">standaloneEnabled=true   </span><br><span class="line">admin.enableServer=true</span><br><span class="line">server.1=0.0.0.0:2888:3888;2181</span><br><span class="line">server.2=zoo2:2888:3888;2181</span><br><span class="line">server.3=zoo3:2888:3888;2181</span><br></pre></td></tr></table></figure>

<h4 id="zkClient"><a href="#zkClient" class="headerlink" title="zkClient"></a>zkClient</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"># 连接服务器进入Bash 模式</span><br><span class="line">./zkCli.sh -server &lt;ip&gt;:&lt;port&gt;</span><br><span class="line"></span><br><span class="line"># 创建节点</span><br><span class="line">create /test &quot;hello zookeeper&quot;</span><br><span class="line"></span><br><span class="line"># 查询节点</span><br><span class="line">get /test</span><br><span class="line"></span><br><span class="line"># 删除节点</span><br><span class="line">delete /test</span><br></pre></td></tr></table></figure>

<h3 id="Dubbo"><a href="#Dubbo" class="headerlink" title="Dubbo"></a>Dubbo</h3><h4 id="官网-1"><a href="#官网-1" class="headerlink" title="官网"></a>官网</h4><blockquote>
<p>中文官网：<a href="http://dubbo.apache.org/zh-cn/" target="_blank" rel="noopener">http://dubbo.apache.org/zh-cn/</a><br>中文文档：<a href="http://dubbo.apache.org/zh-cn/docs/user/quick-start.html" target="_blank" rel="noopener">http://dubbo.apache.org/zh-cn/docs/user/quick-start.html</a></p>
</blockquote>
<h4 id="什么是Dubbo？"><a href="#什么是Dubbo？" class="headerlink" title="什么是Dubbo？"></a>什么是Dubbo？</h4><blockquote>
<p>Apache Dubbo  是一款高性能、轻量级的开源Java RPC框架，它提供了三大核心能力：<strong>面向接口的远程方法调用</strong>，<strong>智能容错和负载均衡</strong>，以及<strong>服务自动注册和发现</strong>。</p>
</blockquote>
<h4 id="Spring-Boot-整合-Dubbo"><a href="#Spring-Boot-整合-Dubbo" class="headerlink" title="Spring Boot 整合 Dubbo"></a>Spring Boot 整合 Dubbo</h4><blockquote>
<p>下面是一个 SpringBoot + Dubbo 的例子，使用Zookeeper作为服务注册与发现中心。  </p>
</blockquote>
<ul>
<li><strong>创建接口项目 service-api</strong>  <blockquote>
<p>定义服务提供接口（该项目使用maven部署到本地仓库，方便后面导入）</p>
</blockquote>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 创建接口</span><br><span class="line">public interface UserService &#123;</span><br><span class="line">    String sayHi();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>创建服务提供者 service-provider</strong><blockquote>
<p>实现接口、提供服务（SpringBoot项目）</p>
</blockquote>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br></pre></td><td class="code"><pre><span class="line"># 导入依赖 （pom.xml）</span><br><span class="line">&lt;!-- SpringBoot-starter Begin --&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.alibaba.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;dubbo-spring-boot-starter&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;0.2.0&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;!-- SpringBoot-starter End --&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.alibaba.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;dubbo-spring-boot-actuator&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;0.2.0&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;!-- local-project Begin--&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.tooi&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;hello-dubbo-service-user-api&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;$&#123;project.version&#125;&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;!-- local-project Begin--&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 配置文件</span><br><span class="line">spring:</span><br><span class="line">  application:</span><br><span class="line">    name: service-provider</span><br><span class="line"></span><br><span class="line">user:</span><br><span class="line">  service:</span><br><span class="line">    version: 1.0.0</span><br><span class="line"></span><br><span class="line">dubbo:</span><br><span class="line">  scan:</span><br><span class="line">    basePackages: com.tooi.service.provider</span><br><span class="line">  application:</span><br><span class="line">    id: service-provider</span><br><span class="line">    name: service-provider</span><br><span class="line">    qos-port: 22222</span><br><span class="line">    qos-enable: true</span><br><span class="line">  protocol:</span><br><span class="line">    id: dubbo</span><br><span class="line">    name: dubbo</span><br><span class="line">    port: 12345</span><br><span class="line">    status: server</span><br><span class="line">  registry:</span><br><span class="line">    id: zookeeper</span><br><span class="line">    address: zookeeper://location:2181?backup=location:2182,location:2183</span><br><span class="line"></span><br><span class="line">management:</span><br><span class="line">  endpoint:</span><br><span class="line">    dubbo:</span><br><span class="line">      enabled: true</span><br><span class="line">    dubbo-shutdown:</span><br><span class="line">      enabled: true</span><br><span class="line">    dubbo-configs:</span><br><span class="line">      enabled: true</span><br><span class="line">    dubbo-services:</span><br><span class="line">      enabled: true</span><br><span class="line">    dubbo-references:</span><br><span class="line">      enabled: true</span><br><span class="line">    dubbo-properties:</span><br><span class="line">      enabled: true</span><br><span class="line">  health:</span><br><span class="line">    dubbo:</span><br><span class="line">      status:</span><br><span class="line">        defaults: memory</span><br><span class="line">        extras: load,threadpool</span><br><span class="line"></span><br><span class="line"># 实现接口 UserServiceImpl.java</span><br><span class="line">@Service(version = &quot;$&#123;user.service.version&#125;&quot;)</span><br><span class="line">public class UserServiceImpl implements UserService &#123;</span><br><span class="line">    @Override</span><br><span class="line">    public String sayHi() &#123;</span><br><span class="line">        return &quot;Hello Dubbo,from port: &quot; + port;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># 开启 Dubbo 容器（启动类）</span><br><span class="line">@SpringBootApplication</span><br><span class="line">public class ServiceProviderApplication &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        SpringApplication.run(HelloDubboServiceUserProviderApplication.class, args);</span><br><span class="line">        // 启动 Provider 容器，注意这里的 Main 是 com.alibaba.dubbo.container 包下的</span><br><span class="line">        Main.main(args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>创建服务消费者 service-consumer</strong></li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br></pre></td><td class="code"><pre><span class="line"># 导入依赖（pom.xml）</span><br><span class="line">&lt;!-- SpringBoot-starter Start --&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.alibaba.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;dubbo-spring-boot-starter&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;0.2.0&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.alibaba.boot&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;dubbo-spring-boot-actuator&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;0.2.0&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;!-- SpringBoot-starter End --&gt;</span><br><span class="line">&lt;!-- local-project Begin--&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.tooi&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;hello-dubbo-service-user-api&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;$&#123;project.version&#125;&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;!-- local-project End--&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 配置文件 </span><br><span class="line">spring:</span><br><span class="line">  application:</span><br><span class="line">    name: service-consumer</span><br><span class="line">server:</span><br><span class="line">  port: 9090</span><br><span class="line"></span><br><span class="line">user:</span><br><span class="line">  service:</span><br><span class="line">    version: 1.0.0</span><br><span class="line"></span><br><span class="line">dubbo:</span><br><span class="line">  scan:</span><br><span class="line">    basePackages: com.tooi.service.consumer.controller</span><br><span class="line">  application:</span><br><span class="line">    id: service-consumer</span><br><span class="line">    name: service-consumer</span><br><span class="line">  registry:</span><br><span class="line">    id: zookeeper</span><br><span class="line">    address: zookeeper://location:2181?backup=location:2182,location:2183</span><br><span class="line"></span><br><span class="line">endpoints:</span><br><span class="line">  dubbo:</span><br><span class="line">    enabled: true</span><br><span class="line"></span><br><span class="line">management:</span><br><span class="line">  server:</span><br><span class="line">    port: 9091</span><br><span class="line">  health:</span><br><span class="line">    dubbo:</span><br><span class="line">      status:</span><br><span class="line">        defaults: memory</span><br><span class="line">  endpoint:</span><br><span class="line">    dubbo:</span><br><span class="line">      enabled: true</span><br><span class="line">    dubbo-shutdown:</span><br><span class="line">      enabled: true</span><br><span class="line">    dubbo-configs:</span><br><span class="line">      enabled: true</span><br><span class="line">    dubbo-services:</span><br><span class="line">      enabled: true</span><br><span class="line">    dubbo-references:</span><br><span class="line">      enabled: true</span><br><span class="line">    dubbo-properties:</span><br><span class="line">      enabled: true</span><br><span class="line">  endpoints:</span><br><span class="line">    web:</span><br><span class="line">      exposure:</span><br><span class="line">        include: &quot;*&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 调用服务</span><br><span class="line">@RestController</span><br><span class="line">public class UserController &#123;</span><br><span class="line"></span><br><span class="line">    @Reference(version = &quot;$&#123;user.service.version&#125;&quot;)</span><br><span class="line">    private UserService userService;</span><br><span class="line"></span><br><span class="line">    @GetMapping(value = &quot;hi&quot;)</span><br><span class="line">    public String sayHi() &#123;</span><br><span class="line">        System.out.println(userService.sayHi());</span><br><span class="line">        return userService.sayHi();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># 启动项目，访问 http://ip:9090/hi</span><br></pre></td></tr></table></figure>



        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/img/donate.png">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        
        <li>
            <a target="_blank"  href="https://github.com/Tooi6">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-github"></i>
                            </span>
            </a>
        </li>
        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a href="https://github.com/Tooi6" target="_blank" rel="noopener">Tooi6</a></span>
        <span>/</span>
        
        <span><a href="#">Tooi-Blog</a></span>
        <span>/</span>
        
        <span><a href="#">It helps SEO</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>  Theme <a href="https://github.com/aircloud/hexo-theme-aircloud" target="_blank" rel="noopener">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="/js/index.js"></script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




</html>
